# -- coding: utf-8 --

import pymysql

def open_conn():
    global conn
    global cursor

    conn = pymysql.connect(
        host='172.16.3.242',
        user = 'root',
        password = 'chebei@hz',
        db = 'ycb_test',
        charset = 'utf8'
    )
    cursor = conn.cursor()

def close_conn():
    cursor.close()
    conn.commit()
    conn.close()

def query_students_fetchone():
    sql = 'SELECT * FROM students'
    rows = cursor.execute(sql)
    print('There are %d students' % rows)

    for i in range(rows):
        student = cursor.fetchone()
        print(student)

def add_student(sno, name, age):
    sql = 'INSERT INTO students(sno, name, age) VALUES("%s", "%s", %d)' % (sno, name, age)
    rows = cursor.execute(sql)
    print('Insert %d students' % rows)

def update_student(sno, name, age):
    sql = 'UPDATE students SET name="%s", age=%d WHERE sno="%s"' % (name, age, sno)
    rows = cursor.execute(sql)
    print('UPDATE %d students' % rows)

def delete_student(sno):
    sql = 'DELETE FROM students WHERE sno="%s"' % (sno)
    rows = cursor.execute(sql)
    print('DELETE %d students' % rows)